﻿using System;
using System.Text;
using AnjLab.SyncIT.Core.Api;
using AnjLab.SyncIT.Core.Data.Outlook;

namespace AnjLab.SyncIT.Core.Data.DotProject
{
    public class Task : ISourceTask
    {
        public string ID { get; set; }
        public string Summary { get; set; }
        public string Owner { get; set; }
        public string OwnerID { get; set; }
        public User[] ResponsibleUsers { get; set; }
        public string ProjectName { get; set; }
        public string ProjectID { get; set; }
        public string Status { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public int Progress { get; set; }
        public string Priority { get; set; }
        public Importance Importance { get; set; }
        public string Description { get; set; }
        public string UserID { get; set; }
        public StatusCode StatusCode { get; set; }
        public string Contacts { get; set; }
        public bool Milestone { get; set; }
        public string ShortDateFormat { get; set; }

        public string ResponsibleUsersString
        {
            get
            {
                var builder = new StringBuilder();
                bool first = true;
                foreach (var user in ResponsibleUsers)
                {
                    if (!first)
                    {
                        builder.Append(", ");
                    }
                    builder.Append(user.DisplayName);
                    first = false;
                }

                if (first)
                {
                    builder.Append("None");
                }
                return builder.ToString();
            }
        }

        public string DescriptionText
        {
            get { return string.IsNullOrEmpty(Description) ? "No Description" : Description; }
        }

        public bool IsClosed
        {
            get
            {
                return Progress == 100 
                    || StatusCode == DotProject.StatusCode.Inactive;
            }
        }

        public bool IsAssignedToMe(Source source)
        {
            //  It is my task if I'm the task owner or I'm one of the responsible users for the task
            return string.Equals(Owner, source.UserName, StringComparison.InvariantCultureIgnoreCase)
                   || string.Equals(OwnerID, UserID, StringComparison.InvariantCultureIgnoreCase)
                   || AmIResponsible;
        }

        public void InitializeOutlookTask(IOutlookItem outlookTask)
        {
        }

        public void UpdateOutlookTask(IOutlookItem outlookTask)
        {
            outlookTask.PercentComplete = Progress;
            outlookTask.StartDate = ParseDotProjectDate(StartDate);
            outlookTask.DueDate = ParseDotProjectDate(EndDate);
        }

        public DateTime ParseDotProjectDate(string date)
        {
            return DateTime.ParseExact(date, ShortDateFormat, Library.InvariantCulture);
        }

        public string DateToDotProjectDateString(DateTime date)
        {
            return date.ToString(ShortDateFormat, Library.InvariantCulture);
        }

        private bool AmIResponsible
        {
            get
            {
                foreach (var user in ResponsibleUsers)
                {
                    if (string.Equals(user.ID, UserID, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return true;
                    }
                }
                return false;
            }
        }
    }

    public enum StatusCode
    {
        Active,
        Inactive
    }

    public class User
    {
        public string ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string DisplayName
        {
            get { return FirstName + " " + LastName; }
        }
    }
}
